package com.feicheng.controller.shop.admin.store;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.constant.Constant;
import com.feicheng.service.IFCheckCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 设备接口
 * 型号：CR07EUW（见设备背面）
 */
@RestController("/shop/admin/store/new")
@RequestMapping("/shop/admin/store/new")
@Slf4j
public class NewAccessController {

    @Autowired
    IFCheckCodeService ifCheckCodeService;
    //扫码
    @RequestMapping(value = "/CheckCode", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String CheckCode(@RequestBody String QCodeAccessVal) throws UnsupportedEncodingException {
        String param = URLDecoder.decode(QCodeAccessVal,"UTF-8");
        JSONObject qCedeAccessValByCut = JSONObject.parseObject(param);
        //去除双引号
        String qCodeStr = (String) qCedeAccessValByCut.remove(Constant.MODEL_NAME.QCODEACCESSVAL.CODEVAL);
        if( 2>= qCodeStr.length()){
            JSONObject closeDor = new JSONObject();
            closeDor.put(Constant.ENTITY.ACCESS.U_ID, qCedeAccessValByCut.get(Constant.MODEL_NAME.QCODEACCESSVAL.UID));
            closeDor.put(Constant.MODEL_NAME.QCODEACCESSVAL.STATUS, 0);
            return closeDor.toJSONString();
        }
        qCodeStr = qCodeStr.substring(1,qCodeStr.length()-1);
        qCedeAccessValByCut.put(Constant.MODEL_NAME.QCODEACCESSVAL.CODEVAL, qCodeStr);
        return ifCheckCodeService.ifOpenDor(qCedeAccessValByCut);
    }
    
    private SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    //心跳方法
    @RequestMapping(value = "/IsConnect", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String isConnect() {
        //获取服务器时间
        String date = s.format(Calendar.getInstance().getTime());
        //返回服务器时间给设备
        return "{\"DateTime\":\""+date+"\"}";
    }
    //设备轮询命令
    @RequestMapping(value = "/QueryCmd", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String QueryCmd(@RequestBody String inJSON) {
        JSONObject inObject = JSONObject.parseObject(inJSON);
        String ViewId = inObject.getString(Constant.MODEL_NAME.CMD.VIEW_ID);
        String UID = inObject.getString(Constant.MODEL_NAME.CMD.UID);
        String UKey = inObject.getString(Constant.MODEL_NAME.CMD.UKEY);
        return ifCheckCodeService.accessGetCmd(ViewId, UID, UKey);
    }
    //设备执行命令结果回调
    @RequestMapping(value = "/QueryCmdPostData", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String QueryCmdPostData(@RequestBody String inJSON) {
        JSONObject inObject = JSONObject.parseObject(inJSON);
        Long CmdID = inObject.getLong(Constant.MODEL_NAME.CMD.CMD_ID);
        Integer CmdStatus = inObject.getInteger(Constant.MODEL_NAME.CMD.CMD_STATUS);
        return ifCheckCodeService.removeCmd(CmdID, CmdStatus);
    }
}
